package com.ctshk.rpc.order.custom.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ctshk.rpc.order.custom.dto.order.CustomOrderDiscountListDTO;
import com.ctshk.rpc.order.custom.entity.CustomizedOrderDiscount;
import com.ctshk.rpc.order.custom.provider.CustomOrderDiscountProvider;
import com.ctshk.rpc.order.custom.req.order.CustomOrderIdReq;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;

import java.math.BigDecimal;

/**
 * <p>
 * 折扣记录 Mapper 接口
 * </p>
 *
 * @author 叶冠峰
 * @since 2021-02-22
 */
public interface CustomizedOrderDiscountMapper extends BaseMapper<CustomizedOrderDiscount> {

    /**
     * 折扣记录列表
     * @param page
     * @param req
     * @return
     */
    @SelectProvider(type = CustomOrderDiscountProvider.class, method = "selectListBySelective")
    IPage<CustomOrderDiscountListDTO> selectListBySelective(@Param("page") IPage<CustomOrderIdReq> page, @Param("req") CustomOrderIdReq req);

    /**
     * 该用户是否使用过折扣
     * @param orderId
     * @param touristId
     * @return
     */
    @Select("SELECT *\n" +
            "FROM t_customized_order_discount\n" +
            "WHERE order_id = #{order_id} AND is_deleted = 0 AND tourist_id = #{tourist_id} AND check_status != 4\n" +
            "LIMIT 1")
    CustomizedOrderDiscount queryByTourist(@Param("order_id") Long orderId, @Param("tourist_id") Long touristId);

    /**
     * 查询优惠金额
     * @param orderId
     * @return
     */
    @Select("SELECT SUM(discount_amount) AS discountAmount\n" +
            "FROM t_customized_order_discount\n" +
            "WHERE order_id = #{order_id} AND check_status = 3 AND is_deleted = 0")
    BigDecimal sumAmountByOrder(@Param("order_id")Long orderId);

    /**
     * 查询旅客折扣金额
     * @param orderId
     * @param touristId
     * @return
     */
    @Select("SELECT SUM(discount_amount) \n" +
            "FROM t_customized_order_discount\n" +
            "WHERE order_id = #{order_id} AND tourist_id = #{tourist_id} AND is_deleted = 0")
    BigDecimal sumAmountByTourist(@Param("order_id")Long orderId, @Param("tourist_id")Long touristId);
}
